#ifndef MSCOMMON_H
#define MSCOMMON_H
#ifdef __cplusplus
   extern "C"{
#endif
#include "mscommon_inner.h"
/*****************************************************************************
DLL链接符号
*****************************************************************************/
#define ms_dllsymbol	DLL_SYMBOL		

/*****************************************************************************
错误返回
*****************************************************************************/
#define ms_averror(e) 		(-(e))
#define ms_neterrno 		ms_averror(errno)
#define ms_strerr 			(strerror(errno))

/*****************************************************************************
系统功能
*****************************************************************************/
#define ms_assert(expression) 	assert(expression)
#define ms_sprintf				sprintf
#define ms_snprintf				snprintf
#define ms_strcat				strcat
#define ms_remove 				remove
#define ms_rename				rename
#define ms_popen				popen
#define ms_pclose				pclose
#define ms_fopen				fopen
#define ms_fread				fread
#define ms_fwrite				fwrite
#define ms_fclose				fclose
#define ms_mkdir(dir)			mst_mkdir(dir)
#define ms_open					mst_open
#define ms_read					mst_read
#define ms_write				mst_write
#define ms_lseek				mst_lseek
#define ms_close				mst_close
#define ms_access				mst_access
#define ms_rmdir(rm_cmd,dirname) ms_sprintfs(rm_cmd,"rm   -fr   %s",dirname); system(rm_cmd);
#define ms_sleep(sencod)		mst_sleep(sencod)
#define ms_msleep(ms)			mst_usleep(ms*1000)
#define ms_usleep(us)			mst_usleep(us)

#define ms_atoi(buf) 			atoi(buf)
#define ms_atol(buf)			atol(buf)
#define ms_exit(ret)			exit(ret)
#define ms_exitErr				ms_exit(1)
#define ms_exitOk				ms_exit(0)
/*****************************************************************************
最大最小值
*****************************************************************************/
#define ms_min(a,b) 			((a) > (b) ? (b) : (a))
#define ms_max(a,b) 			((a) > (b) ? (a) : (b))
#define ms_min3(a,b,c) 			ms_min(ms_min(a,b),c)
#define ms_max3(a,b,c) 			ms_max(ms_max(a,b),c)
#define ms_min4(a,b,c,d) 		ms_min(ms_min3(a,b,c) ,d)
#define ms_min5(a,b,c,d,e) 		ms_min(ms_min4(a,b,c,d),e)
#define ms_min6(a,b,c,d,e,f) 	ms_min(ms_min5(a,b,c,d,e),f)
#define ms_min7(a,b,c,d,e,f,g) 	ms_min(ms_min6(a,b,c,d,e,f),g)
#define ms_min8(a,b,c,d,e,f,g,h) 	ms_min(ms_min7(a,b,c,d,e,f,g),h)

/*****************************************************************************
网络和主机字节的转换
*****************************************************************************/
#define ms_network_8b(buf,val)		NETWORK_8BYTE(buf,val)
#define ms_network_6b(buf,val)		NETWORK_6BYTE(buf,val)
#define ms_network_4b(buf,val)		NETWORK_4BYTE(buf,val)
#define ms_network_3b(buf,val)		NETWORK_3BYTE(buf,val)
#define ms_network_2b(buf,val)		NETWORK_2BYTE(buf,val)
#define ms_network_4b_lf(buf,val)	NETWORK_4BYTE_LF(buf,val)
#define ms_network_2b_lf(buf,val)	NETWORK_2BYTE_LF(buf,val)
#define ms_host_8b(buf)	 			HOST_8BYTE(buf)
#define ms_host_4b(buf)	 			HOST_4BYTE(buf)
#define ms_host_3b(buf)	 			HOST_3BYTE(buf)
#define ms_host_2b(buf)	 			HOST_2BYTE(buf)

/*****************************************************************************
10进制到16进制的转换
*****************************************************************************/
#define ms_10T16_08bit(num)	((num/10<<4)|(num%10))
#define ms_10T16_12bit(num)	((num/100<<8)|((num%100)/10<<4)|((num%100)%10))
		
/*****************************************************************************
命令行
*****************************************************************************/
#define ms_system(cmdline) 					system(cmdline)
#define ms_cmd(cmdbuf,fmt,arg...) 			SYS_CMDLINE(cmdbuf,fmt,##arg)
#define ms_cmdRet(cmdline)					mst_innerapi_cmdRet(cmdline)
#define ms_cmdRead(cmdline,info ,maxlen)	mst_innerapi_cmdReadByPipe(cmdline,info,maxlen)
#define ms_cmdRead2(cmdline,info ,maxlen)	mst_innerapi_cmdReadByTmp(cmdline,info,maxlen)
#define ms_cmdIsGrep(cmdline)				mst_innerapi_cmdIsGrep (cmdline)
#define ms_cmdKill(cmdline)					mst_innerapi_kill(cmdline);
#define ms_cmdCp(cmdbuf,fm,to)  			SYS_CMDCP(cmdbuf,fm,to)
#define ms_cmdCpBin(cmdbuf,fm,to) 			SYS_CMDCP_BIN(cmdbuf,fm,to)

/*****************************************************************************
字符串或BUF处理函数
*****************************************************************************/
//BUF内容和大小
#define ms_strlen(buf)						strlen(buf)
#define ms_buflen(buf)						ms_strlen(buf)
#define ms_bufsize(buf)						sizeof(buf)
//BUF有效性
#define ms_bufUnvalid(buf)					((ms_null==buf)||(0==ms_buflen(buf)))
//数值大小
#define ms_arrayElems(a)					(sizeof(a) / sizeof((a)[0]))
//字符串拼接
#define ms_strcats(tbuf,obuf,fmt,arg...)	CMD_STRCAT(tbuf,obuf,fmt,##arg)
#define ms_strcat2(obuf, msstring);			if(ms_false==msstr_api_isCasestr(obuf, msstring)){ms_strcat(obuf,msstring);}
//字符串打印
#define ms_slprintf							slprintf
#define ms_snprint							mst_snprint
#define ms_snprintfs(outbuf,len,fmt,arg...)	CMD_SNPRINTF(outbuf,len,fmt,##arg)
#define ms_snprints(outbuf,fmt,arg...)		ms_snprintfs(outbuf, ms_bufsize(outbuf), fmt,##arg);
#define ms_sprintfs(outbuf,fmt,arg...)		CMD_SPRINTF(outbuf,fmt,##arg)
//字符串拷贝
#define ms_strcpy(str1,str2)				CMD_STRCPY(str1,str2)
//指针初或BUF始化
#define ms_memset(addr,val,len)				memset(addr,val,len);
#define ms_memset0(addr,len)				memset(addr,0,len);
#define ms_stru0(buf,type)					memset(buf,0,sizeof(type));
#define ms_buf0(buf)						memset(buf,0,sizeof(buf));
#define ms_memcpy(addr1,addr2,len)			memcpy(addr1,addr2,len);
#define ms_memcpy_size(addr1,addr2)			memcpy(addr1,addr2,sizeof(addr2));
//addr1和addr2的len字节进行内容交换
#define ms_bufSwitch(addr1,addr2,len)		mst_innerapi_bufSwitch(addr1,addr2,len);
#define ms_bufOffset(buf,offset_byte,len)	mst_innerapi_bufOffset(buf,offset_byte,len) ; 
/*****************************************************************************
HLS处理
*****************************************************************************/
#define ms_getHlsName(hls_url,vodpath, programname)			mst_innerapi_getOutName(hls_url,vodpath,".m3u8",programname);
#define ms_getHlsPath(hls_url,vodpath, hls_path)			mst_innerapi_getOutPath(hls_url,vodpath,".m3u8", hls_path);
/*****************************************************************************
DASH处理
*****************************************************************************/
#define ms_getDashName(dash_url,vodpath, programname)		mst_innerapi_getOutName(dash_url,vodpath,".mpd",programname);
#define ms_getDashPath(dash_url,vodpath, dash_path)			mst_innerapi_getOutPath(dash_url,vodpath,".mpd", dash_path);
/*****************************************************************************
字符串比较
*****************************************************************************/
#define ms_strncmp_eq(str1,str2,len)	(!strncmp(str1,str2,len))
#define ms_strncmp_neq(str1,str2,len)	(strncmp(str1,str2,len))
#define ms_strncmp_seq(str1,str2)		(ms_strncmp_eq(str1,str2,strlen(str2)))
#define ms_strncmp_sneq(str1,str2)		(ms_strncmp_neq(str1,str2,strlen(str2)))
#define ms_strncmp_saeq(str1,str2)		(ms_strncmp_eq(str1,str2,strlen(str2)) && ms_strncmp_eq(str2,str1,strlen(str1)))
#define ms_strncmp_snaeq(str1,str2)		(!ms_strncmp_saeq(str1,str2))


/*****************************************************************************
内存分配和释放
*****************************************************************************/
#define ms_malloc(size,name)						MS_MALLOC(size,name)
#define ms_mallocDes(size,name,des)				MS_MALLOC_DES(size,name,des)
#define ms_malloc_ret(ret,item,size,name)			MALLOC_VAL_RET_NODBG(item,size,name,ret)
#define ms_malloc_retErr(ret,item,size,name)		MALLOC_VAL_RET(item,size,name,ret)
#define ms_mallocDes_retErr(ret,item,size,name,des)	MALLOCDES_VAL_RET(item,size,name,des,ret)
#define ms_malloc_noRet(item,size,name)				MALLOC_VAL_NORET(item,size,name)
#define ms_mallocDes_noRet(item,size,name,des)		MALLOCDES_VAL_NORET(item,size,name,des)
#define ms_malloc_goto(goval,item,size,name)		MALLOC_VAL_GOTO(goval,item,size,name)
#define ms_mallocDes_goto(goval,item,size,name,des)	MALLOCDES_VAL_GOTO(goval,item,size,name,des)
#define ms_malloc_gotoRet(goval,ret,item,size,name)	MALLOC_VAL_RETGOTO(goval,ret,item,size,name)

#define ms_calloc_ret(item,size,name,ret)			CALLOC_VAL_RET_NODBG(item,size,name,ret)
#define ms_calloc_retErr(ret,item,size,name)		CALLOC_VAL_RET(item,size,name,ret)
#define ms_calloc_noRet(item,size,name)				CALLOC_VAL_NORET(item,size,name)
#define ms_calloc_goto(goval,item,size,name)		CALLOC_VAL_GOTO(goval,item,size,name)
#define ms_calloc_retGoto(goval,ret,item,size,name)	CALLOC_VAL_RETGOTO(goval,ret,item,size,name)

#define ms_free(addr)		MS_FREE(addr)
#define ms_freep(addr)		MS_FREEP(addr)
#define ms_deMalloc(item)  	DEMALLOC_VAL(item)
#define ms_showBlock()  	MS_SHOWBLOCK()

/*****************************************************************************
逻辑比较
*****************************************************************************/
#define  ms_equal(var1,var2)		(var1==var2) 
#define  ms_noEqual(var1,var2)		(var1!=var2) 
#define  ms_lessThan(var,min)		(var<min) 
#define  ms_moreThan(var,max)		(var>max)   
#define  ms_noLess(var,min)			( !ms_lessThan(var,min)  )
#define  ms_noMore(var,max)			( !ms_moreThan(var,max)   )
//[min,max],大于等于min且小于等于max
#define  ms_range(var,min,max)		( ms_noLess(var,min) &&ms_noMore(var,max) )	
//(min,max),大于min且小于max
#define  ms_range2(var,min,max)		( ms_moreThan(var,min) && ms_lessThan(var,max) )
//大于max或小于min
#define  ms_outRange(var,min,max)	( !ms_range(var, min, max))
//大于等于max或小于等于min
#define  ms_outRange2(var,min,max)	( !ms_range2(var, min, max))
//取绝对值
#define	 ms_abs(x)	abs(x)
/*****************************************************************************
自减
*****************************************************************************/
#define ms_sdd(num)	MS_SDD(num)	
#define ms_udd(num)	MS_UDD(num)	

/*****************************************************************************
MAC地址转换为字符串
*****************************************************************************/
#define ms_mac2str( buf, mac_buf )  \
	ms_sprintf( mac_buf, "%02x:%02x:%02x:%02x:%02x:%02x",(unsigned char) *buf, (unsigned char)(*(buf+1)), \
	(unsigned char)(*(buf+2)), (unsigned char)(*(buf+3)), (unsigned char)(*(buf+4)), (unsigned char)(*(buf+5))); \
	mac_buf[17] = 0;

/*****************************************************
URL格式
	ms_urlFormat("/home/3.mp3","mp3")
	ms_urlFormat("http://ip:port/1/1.m3u8","m3u8")
*****************************************************/
#define ms_urlFormat(url,filter_str)  	ms_strncmp_seq((&url[ms_buflen(url)-ms_buflen(filter_str)]), filter_str)

/*****************************************************
字节或位相关
*****************************************************/
#define ms_perBit 	(1024)
#define ms_perByte 	(1024)

#define ms_b		1
#define ms_kb 		(ms_perBit*ms_b)
#define ms_mb		(ms_perBit*ms_kb)
#define ms_gb		(ms_perBit*ms_mb)

#define ms_B		1
#define ms_KB 		(ms_perByte*ms_B)
#define ms_MB		(ms_perByte*ms_KB)
#define ms_GB		(ms_perByte*ms_MB)

#define ms_B2b(byte)	(8*byte)
#define ms_b2B(bit)		(bit/8)

#define ms_bkmg_cmd32(bit,wh,wl)			ms_bitbyte32((bit/wh), ((bit%wh)/wl))
#define ms_bkmg_cmd64(bit,wh,wl)			ms_bitbyte64((bit/wh), ((bit%wh)/wl))
#define ms_bkmg_cmd32_unit(bit,wh,wl,str)	ms_bitbyte32_unit((bit/wh), ((bit%wh)/wl),str)
#define ms_bkmg_cmd64_unit(bit,wh,wl,str)	ms_bitbyte64_unit((bit/wh), ((bit%wh)/wl),str)
//b
#define ms_b32_unit(bit)		ms_bkmg_cmd32_unit(bit,ms_b,1,"b")
#define ms_b64_unit(bit)		ms_bkmg_cmd64_unit(bit,ms_b,1,"b")
//kb
#define ms_b2kb32(bit)			ms_bkmg_cmd32(bit,ms_kb,ms_b)
#define ms_b2kb64(bit)			ms_bkmg_cmd64(bit,ms_kb,ms_b)
#define ms_b2kb32_unit(bit)		ms_bkmg_cmd32_unit(bit,ms_kb,ms_b,"Kb")
#define ms_b2kb64_unit(bit)		ms_bkmg_cmd64_unit(bit,ms_kb,ms_b,"Kb")
//mb
#define ms_b2mb32(bit)			ms_bkmg_cmd32(bit,ms_mb,ms_kb)
#define ms_b2mb64(bit)			ms_bkmg_cmd64(bit,ms_mb,ms_kb)
#define ms_b2mb32_unit(bit)		ms_bkmg_cmd32_unit(bit,ms_mb,ms_kb,"Mb")
#define ms_b2mb64_unit(bit)		ms_bkmg_cmd64_unit(bit,ms_kb,ms_kb,"Mb")
//gb
#define ms_b2gb32(bit)			ms_bkmg_cmd32(bit,ms_gb,ms_mb)
#define ms_b2gb64(bit)			ms_bkmg_cmd64(bit,ms_gb,ms_mb)
#define ms_b2gb32_unit(bit)		ms_bkmg_cmd32_unit(bit,ms_gb,ms_mb,"Gb")
#define ms_b2gb64_unit(bit)		ms_bkmg_cmd64_unit(bit,ms_gb,ms_mb,"Gb")
//B
#define ms_B32_unit(byte)		ms_bkmg_cmd32_unit(byte,ms_B,1,"B")
#define ms_B64_unit(byte)		ms_bkmg_cmd64_unit(byte,ms_B,1,"B")
//KB
#define ms_B2KB32(byte)			ms_bkmg_cmd32(byte,ms_KB,ms_B)
#define ms_B2KB64(byte)			ms_bkmg_cmd64(byte,ms_KB,ms_B)
#define ms_B2KB32_unit(byte)	ms_bkmg_cmd32_unit(byte,ms_KB,ms_B,"KB")
#define ms_B2KB64_unit(byte)	ms_bkmg_cmd64_unit(byte,ms_KB,ms_B,"KB")
//MB
#define ms_B2MB32(byte)			ms_bkmg_cmd32(byte,ms_MB,ms_KB)
#define ms_B2MB64(byte)			ms_bkmg_cmd64(byte,ms_MB,ms_KB)
#define ms_B2MB32_unit(byte)	ms_bkmg_cmd32_unit(byte,ms_MB,ms_KB,"MB")
#define ms_B2MB64_unit(byte)	ms_bkmg_cmd64_unit(byte,ms_MB,ms_KB,"MB")
//GB
#define ms_B2GB32(byte)			ms_bkmg_cmd32(byte,ms_GB,ms_MB)	
#define ms_B2GB64(byte)			ms_bkmg_cmd64(byte,ms_GB,ms_MB)
#define ms_B2GB32_unit(byte)	ms_bkmg_cmd32_unit(byte,ms_GB,ms_MB,"GB")
#define ms_B2GB64_unit(byte)	ms_bkmg_cmd64_unit(byte,ms_GB,ms_MB,"GB")

#define ms_KB2GB32(byte)		ms_bkmg_cmd32(byte,(ms_perByte*ms_perByte),ms_perByte)	
#define ms_KB2GB64(byte)		ms_bkmg_cmd64(byte,(ms_perByte*ms_perByte),ms_perByte)	
#define ms_KB2GB32_unit(byte)	ms_bkmg_cmd32_unit(byte,(ms_perByte*ms_perByte),ms_perByte,"GB")
#define ms_KB2GB64_unit(byte)	ms_bkmg_cmd64_unit(byte,(ms_perByte*ms_perByte),ms_perByte,"GB")	

//Byte/Bit to string
#define ms_Bb32AUTO_unit(byte,falg_bit) 	mst_innerapi_bit2str_64((char[32]){0},byte, falg_bit)	
#define ms_Bb64AUTO_unit(byte,falg_bit)		mst_innerapi_bit2str_32((char[32]){0},byte, falg_bit)

#define ms_BYTE32AUTO_unit(byte) 			ms_Bb32AUTO_unit(byte, ms_false)	
#define ms_BYTE64AUTO_unit(byte)			ms_Bb64AUTO_unit(byte, ms_false)
#define ms_BIT32AUTO_unit(bit) 				ms_Bb32AUTO_unit(bit, ms_true)	
#define ms_BIT64AUTO_unit(bit)				ms_Bb64AUTO_unit(bit, ms_true)

/*****************************************************
通用目录和文件
*****************************************************/
#define PATH_SHARE_MEM    		"/dev/shm"
#define PATH_PID				"/var/run"

#define PATH_LOG_MSCORE			"/var/log/mscore"
#define PATH_LOG_MSNGINX		"/usr/local/nginx"
#define PATH_LOG_MSSRS			"/usr/local/mssrs/objs/"
#define PATH_LOG_MSTOMCAT		"/usr/local/mstomcat/logs/"

#define FILE_LOG_WARING			"/var/log/mscore/msmsg_waring.txt"
#define FILE_LOG_TASKMSG		"/var/log/mscore/msmsg_task.txt"

#define FILE_LOG_MSNETSAFE		"/var/log/msnetsafe.log"
#define FILE_LOG_CLAMAV			"/var/log/clamav/logs/scan.log"

#ifndef MSCOMMON_INNER_C
extern ms_void 
	mscommon_api_info(ms_string ms_out pbuf);
extern ms_string 
	mscommon_api_version(ms_void);
#endif
#ifdef __cplusplus
}
#endif
#endif
